package com.example.loginproject.view.viewModels

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.example.loginproject.models.User
import com.example.loginproject.user.PasswordType
import com.example.loginproject.user.UserManager
import androidx.lifecycle.AndroidViewModel


class SharedViewModel(application: Application):AndroidViewModel(application)  {
   private  var userManager = UserManager.sharedInstance(application.applicationContext)
    val showChange = MutableLiveData(false)
    //按钮是否可以点击
    var loginBtnIsEnable =MutableLiveData(false)
    var registBtnIsEnable =MutableLiveData(false)
    var loginStata = MutableLiveData(LoginStata.Default)
    val user = User()
    val loginedUser = MutableLiveData(User())
    //postValue设置值
     init{
        showChange.postValue(userManager.hasUser())
    }
    fun changeButtonStata(stata: ButtonStata){
       loginBtnIsEnable.postValue(stata== ButtonStata.Enabled)
    }
    fun changeRegistButtonStata(stata: ButtonStata){
        registBtnIsEnable.postValue(stata== ButtonStata.Enabled)
    }
    fun login(name:String,password:String,type:PasswordType){
       val result = userManager.login(name, password, type)
       val stata = if(result) LoginStata.Success else LoginStata.Failure
        loginStata.postValue(stata)
    }
    fun registUser(){
         userManager.registUser(user.name, user.pin,user. pattern)
        showChange.postValue(userManager.hasUser())
    }
    fun loadUserInfo(name:String){
        val user = userManager.getUserInfo(name) ?: return
        loginedUser.postValue(user)
    }
}
enum class ButtonStata{
    Enabled,UnEnabled
}
enum class LoginStata{
    Success,Failure,Default
}